import SwiftUI

struct ShareSheetItem: Identifiable {
    let id: String
}

// MARK: - 数据模型
struct TimelineMonth: Identifiable {
    let id = UUID()
    let month: String
    var days: [TimelineDay]
}
struct TimelineDay: Identifiable {
    let id = UUID()
    let date: String
    let day: String
    let dayOfWeek: String
    var events: [TimelineEvent]
}
struct TimelineEvent: Identifiable {
    let id = UUID()
    let time: String
    let title: String
    let photos: Int
    let location: String
    let thumbnail: String
}

// MARK: - 假数据（迁移自网页端）
let monthlyTimelineData: [TimelineMonth] = [
    TimelineMonth(month: "2024年1月", days: [
        TimelineDay(date: "2024-01-15", day: "15", dayOfWeek: "周一", events: [
            TimelineEvent(time: "08:00", title: "早餐", photos: 5, location: "酒店", thumbnail: "https://images.pexels.com/photos/10967785/pexels-photo-10967785.jpeg"),
            TimelineEvent(time: "09:00", title: "故宫博物院", photos: 25, location: "北京", thumbnail: "https://images.pexels.com/photos/9373300/pexels-photo-9373300.jpeg"),
            TimelineEvent(time: "11:30", title: "太和殿", photos: 12, location: "故宫", thumbnail: "https://images.pexels.com/photos/5504722/pexels-photo-5504722.jpeg"),
            TimelineEvent(time: "12:30", title: "御花园", photos: 18, location: "故宫", thumbnail: "https://images.pexels.com/photos/4241550/pexels-photo-4241550.jpeg"),
            TimelineEvent(time: "14:30", title: "午餐时光", photos: 8, location: "王府井", thumbnail: "https://images.pexels.com/photos/10967785/pexels-photo-10967785.jpeg"),
            TimelineEvent(time: "16:00", title: "王府井大街", photos: 15, location: "王府井", thumbnail: "https://images.pexels.com/photos/2614816/pexels-photo-2614816.jpeg"),
            TimelineEvent(time: "17:30", title: "小吃街", photos: 22, location: "王府井", thumbnail: "https://images.pexels.com/photos/10967785/pexels-photo-10967785.jpeg"),
            TimelineEvent(time: "19:00", title: "晚餐", photos: 11, location: "烤鸭店", thumbnail: "https://images.pexels.com/photos/2181230/pexels-photo-2181230.jpeg"),
            TimelineEvent(time: "20:30", title: "夜景拍摄", photos: 28, location: "天安门", thumbnail: "https://images.pexels.com/photos/3222196/pexels-photo-3222196.jpeg"),
            TimelineEvent(time: "21:45", title: "夜市逛街", photos: 16, location: "前门", thumbnail: "https://images.pexels.com/photos/7993600/pexels-photo-7993600.jpeg"),
            TimelineEvent(time: "22:30", title: "酒吧小憩", photos: 7, location: "三里屯", thumbnail: "https://images.pexels.com/photos/2181230/pexels-photo-2181230.jpeg")
        ]),
        TimelineDay(date: "2024-01-14", day: "14", dayOfWeek: "周日", events: [
            TimelineEvent(time: "10:00", title: "天安门广场", photos: 18, location: "北京", thumbnail: "https://images.pexels.com/photos/5504722/pexels-photo-5504722.jpeg"),
            TimelineEvent(time: "16:00", title: "下午茶", photos: 12, location: "三里屯", thumbnail: "https://images.pexels.com/photos/2181230/pexels-photo-2181230.jpeg")
        ]),
        TimelineDay(date: "2024-01-13", day: "13", dayOfWeek: "周六", events: [
            TimelineEvent(time: "08:30", title: "颐和园", photos: 32, location: "北京", thumbnail: "https://images.pexels.com/photos/4241550/pexels-photo-4241550.jpeg")
        ])
    ]),
    TimelineMonth(month: "2023年12月", days: [
        TimelineDay(date: "2023-12-25", day: "25", dayOfWeek: "周一", events: [
            TimelineEvent(time: "10:00", title: "浅草寺", photos: 31, location: "东京", thumbnail: "https://images.pexels.com/photos/5220043/pexels-photo-5220043.jpeg"),
            TimelineEvent(time: "15:30", title: "传统街区", photos: 16, location: "浅草", thumbnail: "https://images.pexels.com/photos/7993600/pexels-photo-7993600.jpeg")
        ]),
        TimelineDay(date: "2023-12-23", day: "23", dayOfWeek: "周六", events: [
            TimelineEvent(time: "11:00", title: "东京塔", photos: 24, location: "东京", thumbnail: "https://images.pexels.com/photos/2181230/pexels-photo-2181230.jpeg")
        ])
    ])
]

// MARK: - 主视图
struct TimelineView: View {
    @ObservedObject var photoService: PhotoService
    @State private var selectedTimeRange: TimeRange = .all
    
    enum TimeRange: String, CaseIterable {
        case all = "全部"
        case today = "今天"
        case week = "本周"
        case month = "本月"
        case year = "今年"
    }

    @State private var selectedMonthIndex = 0
    @State private var expandedMonths: Set<Int> = [0]
    @State private var expandedDays: Set<String> = []
    @State private var showShareSheet: ShareSheetItem? = nil
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                // 月份切换
                ScrollView(.horizontal, showsIndicators: false) {
                    HStack(spacing: 12) {
                        ForEach(Array(monthlyTimelineData.enumerated()), id: \.offset) { idx, month in
                            Button(action: {
                                selectedMonthIndex = idx
                                withAnimation { expandedMonths = [idx] }
                            }) {
                                Text(month.month)
                                    .font(.headline)
                                    .foregroundColor(selectedMonthIndex == idx ? .white : .accentColor)
                                    .padding(.horizontal, 16)
                                    .padding(.vertical, 8)
                                    .background(selectedMonthIndex == idx ? Color.accentColor : Color(.systemGray6))
                                    .cornerRadius(10)
                            }
                        }
                    }
                    .padding(.horizontal)
                    .padding(.top, 8)
                }
                // 时间轴内容
                ScrollView {
                    LazyVStack(spacing: 18) {
                        let month = monthlyTimelineData[selectedMonthIndex]
                        TimelineMonthSection(
                            month: month,
                            expanded: expandedMonths.contains(selectedMonthIndex),
                            expandedDays: $expandedDays,
                            onShare: { showShareSheet = ShareSheetItem(id: month.month) }
                        )
                    }
                    .padding(.vertical)
                }
            }
            .navigationTitle("时间轴")
            .navigationBarTitleDisplayMode(.large)
            .sheet(item: $showShareSheet) { (item: ShareSheetItem) in
                ActivityView(activityItems: ["分享我的时间轴：\(item.id)"])
            }
        }
    }
}

// MARK: - 月份Section
struct TimelineMonthSection: View {
    let month: TimelineMonth
    let expanded: Bool
    @Binding var expandedDays: Set<String>
    let onShare: () -> Void
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            HStack(spacing: 0) {
                Text(month.month)
                    .font(.title2).bold()
                Spacer()
                Button(action: onShare) {
                    Image(systemName: "square.and.arrow.up")
                        .font(.title3)
                        .foregroundColor(.accentColor)
                        .padding(.leading, 6)
                }
            }
            .padding(.vertical, 8)
            .padding(.horizontal, 8)
            .background(Color(.systemGray6))
            .cornerRadius(12)
            if expanded {
                Divider().padding(.vertical, 4)
                ForEach(month.days) { day in
                    TimelineDaySection(day: day, expanded: expandedDays.contains(day.date), onToggle: {
                        withAnimation {
                            if expandedDays.contains(day.date) {
                                expandedDays.remove(day.date)
                            } else {
                                expandedDays.insert(day.date)
                            }
                        }
                    })
                }
            }
        }
        .padding(.horizontal)
        .padding(.bottom, 2)
    }
}

// MARK: - 日期Section
struct TimelineDaySection: View {
    let day: TimelineDay
    let expanded: Bool
    let onToggle: () -> Void
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            Button(action: onToggle) {
                HStack(spacing: 12) {
                    Text(day.day)
                        .font(.title3).bold()
                        .frame(width: 36)
                    VStack(alignment: .leading, spacing: 2) {
                        Text(day.dayOfWeek)
                            .font(.caption)
                            .foregroundColor(.secondary)
                        Text(day.date)
                            .font(.caption2)
                            .foregroundColor(.secondary)
                    }
                    Spacer()
                    Image(systemName: expanded ? "chevron.down" : "chevron.right")
                        .foregroundColor(.secondary)
                        .font(.title3)
                }
                .padding(.vertical, 8)
                .padding(.horizontal, 8)
                .background(Color(.systemGray6))
                .cornerRadius(10)
            }
            .buttonStyle(PlainButtonStyle())
            if expanded {
                Divider().padding(.vertical, 4)
                ForEach(day.events) { event in
                    TimelineEventCard(event: event)
                }
            }
        }
        .padding(.leading, 16)
        .padding(.bottom, 2)
    }
}

// MARK: - 事件卡片
struct TimelineEventCard: View {
    let event: TimelineEvent
    var body: some View {
        HStack(spacing: 12) {
            if let url = URL(string: event.thumbnail) {
                AsyncImage(url: url) { phase in
                    switch phase {
                    case .empty:
                        ProgressView().frame(width: 56, height: 56)
                    case .success(let image):
                        image.resizable().aspectRatio(1, contentMode: .fill)
                            .frame(width: 56, height: 56)
                            .clipped()
                            .cornerRadius(10)
                    case .failure:
                        Image(systemName: "photo").resizable().frame(width: 56, height: 56)
                    @unknown default:
                        Image(systemName: "photo").resizable().frame(width: 56, height: 56)
                    }
                }
            }
            VStack(alignment: .leading, spacing: 4) {
                Text(event.title)
                    .font(.headline)
                Text(event.time)
                    .font(.caption)
                    .foregroundColor(.secondary)
                Text(event.location)
                    .font(.caption2)
                    .foregroundColor(.secondary)
            }
            Spacer()
            Text("\(event.photos) 张")
                .font(.caption)
                .foregroundColor(.accentColor)
        }
        .padding(10)
        .background(Color(.systemBackground))
        .cornerRadius(14)
        .shadow(color: Color.black.opacity(0.05), radius: 2, x: 0, y: 1)
    }
}

// MARK: - 分享弹窗

#Preview {
    TimelineView(photoService: PhotoService())
} 

 